package com.lyfx.ly.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.lyfx.ly.entity.ClassCourseEntity;
import com.lyfx.ly.entity.StudyRouteActivitiyEntity;
import com.lyfx.ly.service.ClassCourseService;
import com.lyfx.ly.service.StudyRouteActivitiyService;
import com.lyfx.ly.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.Query;

import com.lyfx.ly.dao.StudyRouteDao;
import com.lyfx.ly.entity.StudyRouteEntity;
import com.lyfx.ly.service.StudyRouteService;
import org.springframework.transaction.annotation.Transactional;


@Service("studyRouteService")
public class StudyRouteServiceImpl extends ServiceImpl<StudyRouteDao, StudyRouteEntity> implements StudyRouteService {


    @Autowired
    StudyRouteActivitiyService studyRouteActivitiyService;
    @Autowired
    ClassCourseService classCourseService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<StudyRouteEntity> queryWrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("route_id", key).or().like("route_name", key);
        }
        IPage<StudyRouteEntity> page = this.page(
                new Query<StudyRouteEntity>().getPage(params),
                queryWrapper
        );
        List<StudyRouteEntity> records = page.getRecords();
        List<RouteWithActivityVo> routeId = records.stream().map(item -> {
            RouteWithActivityVo route = new RouteWithActivityVo();
            BeanUtils.copyProperties(item, route);
            List<StudyRouteActivitiyEntity> route_id = studyRouteActivitiyService.list(new QueryWrapper<StudyRouteActivitiyEntity>().eq("route_id", route.getRouteId()));
            List<ActRespVo> collect = route_id.stream().map(routeAct -> {
                ActRespVo actRespVo = new ActRespVo();
                List<String> result = Arrays.asList(routeAct.getActCourse().split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
                List<Long> listLong = JSONArray.parseArray(result.toString(), Long.class);
                List<ClassCourseEntity> entities = classCourseService.listByIds(listLong);
                List<String> coursesList = entities.stream().map(course -> {
                    return course.getName();
                }).collect(Collectors.toList());
                actRespVo.setActCourse(coursesList);
                actRespVo.setActId(routeAct.getId());
                actRespVo.setActShowStatus(routeAct.getShowStatus());
                actRespVo.setActSort(routeAct.getSort());
                actRespVo.setActTitle(routeAct.getActTitle());
                return actRespVo;
            }).sorted((a,b)->{
                return (a.getActSort()==null?0:a.getActSort())-(b.getActSort()==null?0:b.getActSort());
            }).collect(Collectors.toList());
            route.setAct(collect);
            return route;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(routeId);
        return pageUtils;
    }

    @Override
    @Transactional
    public void saveBaseValuse(RouteRespAllVo studyRoute) {
        //1、保存routeForm 基本信息
        RouteForm routeForm = studyRoute.getRouteForm();
        StudyRouteEntity routeEntity = new StudyRouteEntity();
        BeanUtils.copyProperties(routeForm,routeEntity );
        this.save(routeEntity);
        Long routeId = routeEntity.getRouteId();
        //2、保存activities表里的信息
        if(routeId==null){
            throw new RuntimeException("新增失败");
        }
        List<Activities> activities = studyRoute.getActivities();
        List<StudyRouteActivitiyEntity> collect = activities.stream().map(item -> {
            StudyRouteActivitiyEntity studyRouteActivitiyEntity = new StudyRouteActivitiyEntity();
            studyRouteActivitiyEntity.setRouteId(routeId);
            studyRouteActivitiyEntity.setActTitle(item.getTitle());
            studyRouteActivitiyEntity.setActCourse(item.getCourseId() == null ? "" : StringUtils.join( item.getCourseId(), ","));
            return studyRouteActivitiyEntity;
        }).collect(Collectors.toList());
        studyRouteActivitiyService.saveBatch(collect);
    }

    @Override
    @Transactional
    public void removeToall(List<Long> asList) {
        List<StudyRouteEntity> collect = asList.stream().map(item -> {
            StudyRouteEntity studyRouteEntity = new StudyRouteEntity();
            studyRouteEntity.setRouteId(item);
            studyRouteEntity.setShowStatus(0);
            return studyRouteEntity;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
        studyRouteActivitiyService.removeByRouteIds(asList);
    }

    @Override
    public void updateStatus(StudyRouteEntity studyRoute) {
        this.updateById(studyRoute);
        StudyRouteActivitiyEntity studyRouteActivitiyEntity = new StudyRouteActivitiyEntity();
        studyRouteActivitiyEntity.setShowStatus(studyRoute.getShowStatus());
        studyRouteActivitiyService.update(studyRouteActivitiyEntity, new QueryWrapper<StudyRouteActivitiyEntity>().eq("route_id", studyRoute.getRouteId()));
    }

}